﻿/*
 * My Timer - cdReadSaveSetting.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace My_Timer
{
    public class ApplicationSetting
    {
        public static string savePath;
        public static string pathTimersXml;
        public static string pathMyTimerXml;
    }

    public class SaveSetting
    {
        public static void SaveAllSetting()
        {
            // call all save function
            SaveTimerSetting();
            SaveApplicationSetting();
        }

        public static void SaveApplicationSetting()
        {
            try
            {
                // save application setting

                string s_File = ApplicationSetting.pathMyTimerXml;

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.OmitXmlDeclaration = true;
                settings.NewLineChars = Environment.NewLine;

                XmlWriter r_SaveApplicationSetting = XmlWriter.Create(s_File, settings);

                // Opens the document
                r_SaveApplicationSetting.WriteStartDocument();

                // Comment
                r_SaveApplicationSetting.WriteComment("My Timer " + Application.ProductVersion.ToString() + " My Timer.xml");
                r_SaveApplicationSetting.WriteComment("WARNING! Do not manually change this config file. Editing this file may cause My Timer to crash.");

                // <Settings>
                r_SaveApplicationSetting.WriteStartElement("Settings");

                // <StartMinimized value="<StartMinimized>">
                r_SaveApplicationSetting.WriteStartElement("StartMinimized");
                r_SaveApplicationSetting.WriteAttributeString("value", Settings.StartMinimized.ToString());

                // </Settings>
                r_SaveApplicationSetting.WriteEndElement();

                // Ends the document.
                r_SaveApplicationSetting.WriteEndDocument();

                r_SaveApplicationSetting.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while saving settings to My Timer.xml! Your settings are not saved.\n"
                    + "Error message: " + ex.Message, "My Timer", MessageBoxButtons.OK, MessageBoxIcon.Error);
#if DEBUG
                MessageBox.Show("Error caught in SaveApplicationSetting. Error: " + ex.Message);
#endif
            }
        }

        public static void SaveTimerSetting()
        {
            try
            {
                // save timer setting
                string s_File = ApplicationSetting.pathTimersXml;

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.OmitXmlDeclaration = true;
                settings.NewLineChars = Environment.NewLine;

                XmlWriter r_SaveTimerSetting = XmlWriter.Create(s_File, settings);

                // Opens the document 
                r_SaveTimerSetting.WriteStartDocument();

                // Comment
                r_SaveTimerSetting.WriteComment("My Timer " + Application.ProductVersion.ToString() + " Timer.xml");
                r_SaveTimerSetting.WriteComment("WARNING! Do not manually change this config file. Editing this file may cause My Timer to crash.");

                // <Timers>
                r_SaveTimerSetting.WriteStartElement("Timers");

                if (GlobalClass.r_Timers.Count > 0)
                {
                    foreach (int i in GlobalClass.r_Timers.Keys)
                    {
                        // <Timer ID="i">
                        // ID="i" is for debug purpose =P
                        r_SaveTimerSetting.WriteStartElement("Timer");
                        r_SaveTimerSetting.WriteAttributeString("ID", i.ToString());

                        // <Name>Name</Name>
                        r_SaveTimerSetting.WriteStartElement("Name");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].Name);
                        r_SaveTimerSetting.WriteEndElement();

                        // <OnTimerEnd>OnTimerEnd</OnTimerEnd>
                        r_SaveTimerSetting.WriteStartElement("OnTimerEnd");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].OnTimerEnd.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <PopupMsgBox>PopupMsgBox</PopupMsgBox>
                        r_SaveTimerSetting.WriteStartElement("PopupMsgBox");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].PopupMsgBox.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <MsgBoxString>MsgBoxString</MsgBoxString>
                        r_SaveTimerSetting.WriteStartElement("MsgBoxString");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].MsgBoxString);
                        r_SaveTimerSetting.WriteEndElement();

                        // <PlayAudio>PlayAudio</PlayAudio>
                        r_SaveTimerSetting.WriteStartElement("PlayAudio");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].PlayAudio.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <PlayAudioPath>PlayAudioPath</PlayAudioPath>
                        r_SaveTimerSetting.WriteStartElement("PlayAudioPath");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].PlayAudioPath);
                        r_SaveTimerSetting.WriteEndElement();

                        // <Time Days="Days" Hours="Hours" Minutes="Minutes" Seconds="Seconds">
                        r_SaveTimerSetting.WriteStartElement("Time");
                        r_SaveTimerSetting.WriteAttributeString("Days", GlobalClass.r_Timers[i].Days.ToString());
                        r_SaveTimerSetting.WriteAttributeString("Hours", GlobalClass.r_Timers[i].Hours.ToString());
                        r_SaveTimerSetting.WriteAttributeString("Minutes", GlobalClass.r_Timers[i].Minutes.ToString());
                        r_SaveTimerSetting.WriteAttributeString("Seconds", GlobalClass.r_Timers[i].Seconds.ToString());


                        // <TimerState value="timerState" />
                        r_SaveTimerSetting.WriteStartElement("TimerState");
                        r_SaveTimerSetting.WriteAttributeString("value", GlobalClass.r_Timers[i].timerState.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <CountTime Days="tDays" Hours="tHours" Minutes="tMinutes" Seconds="tSeconds" />
                        r_SaveTimerSetting.WriteStartElement("CountTime");
                        r_SaveTimerSetting.WriteAttributeString("Days", GlobalClass.r_Timers[i].tDays.ToString());
                        r_SaveTimerSetting.WriteAttributeString("Hours", GlobalClass.r_Timers[i].tHours.ToString());
                        r_SaveTimerSetting.WriteAttributeString("Minutes", GlobalClass.r_Timers[i].tMinutes.ToString());
                        r_SaveTimerSetting.WriteAttributeString("Seconds", GlobalClass.r_Timers[i].tSeconds.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // </Time>
                        r_SaveTimerSetting.WriteEndElement();

                        // <Possition x="xPossition" y="yPossition" />
                        r_SaveTimerSetting.WriteStartElement("Possition");
                        r_SaveTimerSetting.WriteAttributeString("x", GlobalClass.r_Timers[i].xPossition.ToString());
                        r_SaveTimerSetting.WriteAttributeString("y", GlobalClass.r_Timers[i].yPossition.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <Size x="xSize" y="ySize" />
                        r_SaveTimerSetting.WriteStartElement("Size");
                        r_SaveTimerSetting.WriteAttributeString("x", GlobalClass.r_Timers[i].xSize.ToString());
                        r_SaveTimerSetting.WriteAttributeString("y", GlobalClass.r_Timers[i].ySize.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <Opacity>form_Opacity</Opacity>
                        r_SaveTimerSetting.WriteStartElement("Opacity");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].form_Opacity.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <AlwaysOnTop>form_AlwaysOnTop</AlwaysOnTop>
                        r_SaveTimerSetting.WriteStartElement("AlwaysOnTop");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].form_AlwaysOnTop.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <ShutdownComputer>ShutdownComputer</ShutdownComputer>
                        r_SaveTimerSetting.WriteStartElement("ShutdownComputer");
                        r_SaveTimerSetting.WriteString(GlobalClass.r_Timers[i].ShutdownComputer.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // <openApp value="bool" path="appLocation" />
                        r_SaveTimerSetting.WriteStartElement("openApp");
                        r_SaveTimerSetting.WriteAttributeString("value", GlobalClass.r_Timers[i].openApp.ToString());
                        r_SaveTimerSetting.WriteAttributeString("path", GlobalClass.r_Timers[i].appLocation.ToString());
                        r_SaveTimerSetting.WriteEndElement();

                        // </Timer>
                        r_SaveTimerSetting.WriteEndElement();
                    }
                }

                // </Timers>
                r_SaveTimerSetting.WriteEndElement();

                // Ends the document.
                r_SaveTimerSetting.WriteEndDocument();

                // close writer
                r_SaveTimerSetting.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while saving settings to Timers.xml! Your settings are not saved.\n" 
                    + "Error message: " + ex.Message, "My Timer", MessageBoxButtons.OK, MessageBoxIcon.Error);

#if DEBUG
                MessageBox.Show("Error caught in SaveTimerSetting. Error: " + ex.Message);
#endif
            }
        }
    }

    public class LoadSetting
    {
        public static void LoadAllSettings()
        {
            // load save path first
            LoadSavePath();

            // call all load setting functions
            LoadTimerSetting();
            LoadApplicationSetting();
        }

        public static void LoadApplicationSetting()
        {
            // load application setting
            string s_File = ApplicationSetting.pathMyTimerXml;

            bool isSettingCorrupt = false;

            // set default value first
            Settings.StartMinimized = false;

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;

            if (File.Exists(s_File) == true)
            {
                string handle = "";
                XmlReader r_LoadApplicationSetting = XmlReader.Create(s_File, settings);

                while (r_LoadApplicationSetting.Read())
                {
                    switch (r_LoadApplicationSetting.NodeType)
                    {
                        case XmlNodeType.Element:
                            // start case XmlNodeType.Element

                            switch (r_LoadApplicationSetting.Name.ToLower())
                            {
                                case "startminimized":
                                    handle = "startminimized";

                                    try
                                    {
                                        Settings.StartMinimized = Convert.ToBoolean(r_LoadApplicationSetting.GetAttribute("value"));
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        Settings.StartMinimized = false;

#if DEBUG
                                        MessageBox.Show("Error caught in handle = \"startminimized\";. Error: " + ex.Message);
#endif
                                    }

                                    break;

                                default:
                                    handle = "";

                                    break;
                            }

                            // break case XmlNodeType.Element
                            break;

                        case XmlNodeType.Text:
                            // start case XmlNodeType.Text

                            switch (handle)
                            {
                                default:

                                    break;
                            }

                            // break case XmlNodeType.Text
                            break;
                    }
                }

                r_LoadApplicationSetting.Close();
            }

            if (isSettingCorrupt == true)
            {
                MessageBox.Show("Some settings in My Timer.xml are corrupt! My Timer is unable to load some of your settings.",
                                        "My Timer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static void LoadTimerSetting()
        {
            // load timer setting
            string s_File = ApplicationSetting.pathTimersXml;

            string handle = "";
            bool isSettingCorrupt = false;

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;

            if (File.Exists(s_File) == true)
            {
                XmlReader r_LoadTimerSetting = XmlReader.Create(s_File, settings);

                int i = 0;
                TimersArray t_TimersArray = new TimersArray();

                int tDays = 0, tHours = 0, tMinutes = 0, tSeconds = 0;
                byte timerState = 0;

                while (r_LoadTimerSetting.Read())
                {
                    switch (r_LoadTimerSetting.NodeType)
                    {
                        case XmlNodeType.Element:
                            // start case XmlNodeType.Element
                            switch (r_LoadTimerSetting.Name.ToLower())
                            {
                                case "timer":
                                    handle = "timer";

                                    t_TimersArray = new TimersArray();

                                    i = GlobalClass.timerNextKey;
                                    GlobalClass.timerNextKey++;

                                    //set default value

                                    t_TimersArray.Name = "";
                                    t_TimersArray.Days = 0;
                                    t_TimersArray.Hours = 0;
                                    t_TimersArray.Minutes = 0;
                                    t_TimersArray.Seconds = 0;
                                    t_TimersArray.openApp = false;
                                    t_TimersArray.appLocation = "";
                                    t_TimersArray.form_AlwaysOnTop = false;
                                    t_TimersArray.form_Opacity = 1;
                                    t_TimersArray.IsFormEnable = false;
                                    t_TimersArray.PopupMsgBox = false;
                                    t_TimersArray.MsgBoxString = "";
                                    t_TimersArray.OnTimerEnd = 0;
                                    t_TimersArray.PlayAudio = false;
                                    t_TimersArray.PlayAudioPath = "";
                                    t_TimersArray.ShutdownComputer = 0;
                                    t_TimersArray.xPossition = Screen.PrimaryScreen.Bounds.Width - 250;
                                    t_TimersArray.yPossition = 50;
                                    t_TimersArray.xSize = 200;
                                    t_TimersArray.ySize = 60;
                                    t_TimersArray.iIndexOfKey = 0;
                                    t_TimersArray.tKey = i;
                                    t_TimersArray.timerDeleted = false;
                                    t_TimersArray.timerState = 0;

                                    GlobalClass.r_Timers.Add(i, t_TimersArray);

                                    timerState = 0;
                                    tDays = 0;
                                    tHours = 0;
                                    tMinutes = 0;
                                    tSeconds = 0;

                                    break;

                                case "name":
                                    handle = "name";

                                    break;

                                case "ontimerend":
                                    handle = "ontimerend";

                                    break;

                                case "popupmsgbox":
                                    handle = "popupmsgbox";

                                    break;

                                case "msgboxstring":
                                    handle = "msgboxstring";

                                    break;

                                case "playaudio":
                                    handle = "playaudio";

                                    break;

                                case "playaudiopath":
                                    handle = "playaudiopath";

                                    break;

                                case "time":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.Days = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Days"));
                                        t_TimersArray.Hours = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Hours"));
                                        t_TimersArray.Minutes = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Minutes"));
                                        t_TimersArray.Seconds = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Seconds"));
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.Days = 0;
                                        t_TimersArray.Hours = 0;
                                        t_TimersArray.Minutes = 0;
                                        t_TimersArray.Seconds = 0;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"time\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "timerstate":
                                    handle = "timerstate";

                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        timerState = Convert.ToByte(r_LoadTimerSetting.GetAttribute("value"));
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        timerState = 0;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"timerstate\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "counttime":
                                    handle = "counttime";

                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        tDays = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Days"));
                                        tHours = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Hours"));
                                        tMinutes = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Minutes"));
                                        tSeconds = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("Seconds"));
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;

                                        tDays = 0;
                                        tHours = 0;
                                        tMinutes = 0;
                                        tSeconds = 0;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"counttime\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "possition":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.xPossition = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("x"));
                                        t_TimersArray.yPossition = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("y"));
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.xPossition = Screen.PrimaryScreen.Bounds.Width - 250;
                                        t_TimersArray.yPossition = 50;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"possition\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "size":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.xSize = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("x"));
                                        t_TimersArray.ySize = Convert.ToInt32(r_LoadTimerSetting.GetAttribute("y"));
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.xSize = 235;
                                        t_TimersArray.ySize = 75;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"size\":. Error: " + ex.Message);
#endif
                                    }

                                    if (t_TimersArray.xSize < 30)
                                    {
                                        t_TimersArray.xSize = 30;
                                    }

                                    if (t_TimersArray.ySize < 30)
                                    {
                                        t_TimersArray.ySize = 30;
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "opacity":
                                    handle = "opacity";

                                    break;

                                case "alwaysontop":
                                    handle = "alwaysontop";

                                    break;

                                case "shutdowncomputer":
                                    handle = "shutdowncomputer";

                                    break;

                                case "openapp":
                                    handle = "openapp";

                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.openApp = Convert.ToBoolean(r_LoadTimerSetting.GetAttribute("value"));
                                        t_TimersArray.appLocation = r_LoadTimerSetting.GetAttribute("path");
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.openApp = false;
                                        t_TimersArray.appLocation = "";

#if DEBUG
                                        MessageBox.Show("Error caught in case \"openapp\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                default:
                                    handle = "";

                                    break;
                            }

                            // break case XmlNodeType.Element
                            break;

                        case XmlNodeType.Text:
                            // start case XmlNodeType.Text
                            switch (handle)
                            {
                                case "name":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    t_TimersArray.Name = r_LoadTimerSetting.Value;

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "ontimerend":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.OnTimerEnd = Convert.ToByte(r_LoadTimerSetting.Value);
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.OnTimerEnd = 0;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"ontimerend\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "popupmsgbox":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.PopupMsgBox = Convert.ToBoolean(r_LoadTimerSetting.Value);
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.PopupMsgBox = false;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"popupmsgbox\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "msgboxstring":
                                    t_TimersArray = GlobalClass.r_Timers[i];
                                    t_TimersArray.MsgBoxString = r_LoadTimerSetting.Value;

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "playaudio":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.PlayAudio = Convert.ToBoolean(r_LoadTimerSetting.Value);
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.PlayAudio = false;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"playaudio\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "playaudiopath":
                                    t_TimersArray = GlobalClass.r_Timers[i];
                                    t_TimersArray.PlayAudioPath = r_LoadTimerSetting.Value;

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "opacity":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.form_Opacity = Convert.ToDouble(r_LoadTimerSetting.Value);
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.form_Opacity = 1;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"opacity\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "alwaysontop":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.form_AlwaysOnTop = Convert.ToBoolean(r_LoadTimerSetting.Value);
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.form_AlwaysOnTop = false;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"alwaysontop\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                case "shutdowncomputer":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    try
                                    {
                                        t_TimersArray.ShutdownComputer = Convert.ToByte(r_LoadTimerSetting.Value);
                                    }
                                    catch (Exception ex)
                                    {
                                        isSettingCorrupt = true;
                                        t_TimersArray.ShutdownComputer = 0;

#if DEBUG
                                        MessageBox.Show("Error caught in case \"shutdowncomputer\":. Error: " + ex.Message);
#endif
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                default:
                                    // ?

                                    break;
                            }

                            // break case XmlNodeType.Text
                            break;

                        case XmlNodeType.EndElement:
                            // start case XmlNodeType.EndElement

                            switch (r_LoadTimerSetting.Name.ToLower())
                            {
                                case "time":
                                    t_TimersArray = GlobalClass.r_Timers[i];

                                    if (timerState > 0)
                                    {
                                        // set to paused
                                        t_TimersArray.timerState = 2;

                                        t_TimersArray.tDays = tDays;
                                        t_TimersArray.tHours = tHours;
                                        t_TimersArray.tMinutes = tMinutes;
                                        t_TimersArray.tSeconds = tSeconds;
                                    }
                                    else
                                    {
                                        t_TimersArray.tDays = t_TimersArray.Days;
                                        t_TimersArray.tHours = t_TimersArray.Hours;
                                        t_TimersArray.tMinutes = t_TimersArray.Minutes;
                                        t_TimersArray.tSeconds = t_TimersArray.Seconds;
                                    }

                                    GlobalClass.r_Timers[i] = t_TimersArray;

                                    break;

                                default:
                                    // ?

                                    break;
                            }

                            // break case XmlNodeType.EndElement
                            break;
                    }
                }

                r_LoadTimerSetting.Close();
            }

            if (isSettingCorrupt == true)
            {
                MessageBox.Show("Some settings in Timers.xml are corrupt! My Timer is unable to load some of your settings.",
                                        "My Timer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        
                //MessageBox.Show("Error while loading settings from Timer.xml! Your settings are not loaded.\n"
                    //+ "Error: " + ex.Message, "My Timer", MessageBoxButtons.OK, MessageBoxIcon.Error);
        
        }

        public static void LoadSavePath()
        {
            // set setting save path, if settings file are in .exe directory,
            // run in portable mode

            bool portable = false;

            if (File.Exists("Timers.xml") == true)
            {
                portable = true;
            }

            if (File.Exists("My Timer.xml") == true)
            {
                portable = true;
            }

            if (portable == true)
            {
                ApplicationSetting.savePath = Directory.GetCurrentDirectory();
            }
            else
            {
                ApplicationSetting.savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "TMS Network");
                ApplicationSetting.savePath = Path.Combine(ApplicationSetting.savePath, "My Timer");
            }

            ApplicationSetting.pathMyTimerXml = Path.Combine(ApplicationSetting.savePath, "My Timer.xml");
            ApplicationSetting.pathTimersXml = Path.Combine(ApplicationSetting.savePath, "Timers.xml");

            if (Directory.Exists(ApplicationSetting.savePath) == false)
            {
                Directory.CreateDirectory(ApplicationSetting.savePath);
            }
        }
    }
}